package com.tencent.qcloud.tim.demo.login;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.tencent.qcloud.tim.demo.R;
import com.tencent.qcloud.tim.demo.utils.DBHelper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

public class ZhuCeActivity extends AppCompatActivity {

    private EditText etPhone, etPassword, etPasswordAgain;
    private AppCompatButton btnRegister;
    private CheckBox cbUserAgreement;  // 用户协议复选框
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuce);  // 你的注册界面布局文件

        // 初始化视图
        etPhone = findViewById(R.id.etPhone_register);
        etPassword = findViewById(R.id.etCode_register);
        etPasswordAgain = findViewById(R.id.etCodeAgain);
        btnRegister = findViewById(R.id.btnRegister);

        // 注册按钮点击事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户输入
                String phone = etPhone.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                String passwordAgain = etPasswordAgain.getText().toString().trim();

                // 执行注册逻辑
                register(phone, password, passwordAgain);
            }
        });
    }

    // 注册逻辑
    private void register(String phone, String password, String passwordAgain) {
        // 验证手机号码
        if (phone.isEmpty() || phone.length() != 11) {
            Toast.makeText(this, "请输入有效的手机号码", Toast.LENGTH_SHORT).show();
            return;
        }

        // 验证密码
        if (password.isEmpty()) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // 验证密码确认
        if (!password.equals(passwordAgain)) {
            Toast.makeText(this, "密码和确认密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        // 检查用户协议是否勾选
        if (!cbUserAgreement.isChecked()) {
            Toast.makeText(this, "请先勾选用户协议", Toast.LENGTH_SHORT).show();
            return;
        }
        // 调用 DBHelper 注册方法
        new Thread(new Runnable() {
            @Override
            public void run() {
                int result = DBHelper.registerUser(phone, password);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        switch (result) {
                            case 1:
                                Toast.makeText(ZhuCeActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                // 注册成功后可以跳转到登录界面
                                finish();
                                break;
                            case -1:
                                Toast.makeText(ZhuCeActivity.this, "该手机号已注册", Toast.LENGTH_SHORT).show();
                                break;
                            case 0:
                                Toast.makeText(ZhuCeActivity.this, "注册失败，请稍后重试", Toast.LENGTH_SHORT).show();
                                break;
                            case -2:
                                Toast.makeText(ZhuCeActivity.this, "注册过程中出现错误", Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }
                });
            }
        }).start();
    }

    // 跳转到登录页面
    public void goLoginLink(View view) {
        finish();  // 返回上一页，即登录页面
    }
}
